﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PDFLibNet;
using System.IO;


namespace kiosk
{
    public partial class WizardWindow : Form
    {
       
        private Int32 GroupID;
        private DataBaseHandler dHandler;
        private Int32 itemid;
        private FileStream fs;
        private PDFWrapper mPdfDoc;
        private string m_path;
        private static Int32 m_currentPdfPage = 1;
        public WizardWindow(Int32 gid ,Int32 itemid, DataBaseHandler handler)
        {
            
            InitializeComponent();
            GroupID = gid;
            this.dHandler = handler;
            this.WelComePanel.Visible = true;
            this.showPdfPicBox.AutoScroll = true;
            this.WizardDTPanel.Visible = false;
            
            this.itemid = itemid;

        }
      
        
        private void WelNextBtn_Click(object sender, EventArgs e)
        {


            this.WizardDTPanel.Visible = true;
          
        }

        private void WelCancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      

        private void DTpInBackBtn_Click(object sender, EventArgs e)
        {

            this.WizardDTPanel.Visible = false;
           
        }

        private void DTpInCancelBtn_Click(object sender, EventArgs e)
        {

            this.Close();
            
        }

       
        private void GroupBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void WizardWindowPanel_Load(object sender, EventArgs e)
        {

        }

        private void loadPdfBtn_Click(object sender, EventArgs e)
        {
            // the logic for the load pdf button
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Portable Document Format (*.pdf)|*.pdf";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (this.mPdfDoc != null)
                {
                    this.mPdfDoc.Dispose();
                    this.mPdfDoc = null;
                
                }

                this.mPdfDoc = new PDFLibNet.PDFWrapper();
                this.mPdfDoc.LoadPDF(dlg.FileName);
                this.m_path = dlg.FileName;
                this.mPdfDoc.CurrentPage = 1;
                // Settings for the page
                if (this.mPdfDoc.PageCount > 1)
                {
                    this.nextPageBtn.Enabled = true; 
                    
                }

                //
                this.mPdfDoc.FitToWidth(this.showPdfPicBox.Handle);
            
               

                this.mPdfDoc.RenderPage(this.showPdfPicBox.Handle);

                this.mPdfDoc.CurrentX = 0;
                this.mPdfDoc.CurrentY = 0;
                this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

                using (Graphics g = Graphics.FromImage(_backbuffer))
                {
                    this.mPdfDoc.DrawPageHDC(g.GetHdc());
                    g.ReleaseHdc();
                }

                this.showPdfPicBox.Image = _backbuffer;
               

            }




        }

        private bool LoadFile(string filename, PDFLibNet.PDFWrapper pdfDoc)
        {
            try
            {
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
                //Does not supported by MuPDF.                
                //fs = new System.IO.FileStream(filename, System.IO.FileMode.Open);                
                //return pdfDoc.LoadPDF(fs);                
                bool bRet = pdfDoc.LoadPDF(filename);

                return bRet;
            }
            catch (System.Security.SecurityException)
            {
                return false;

            }
        }

        private void previousPageBtn_Click(object sender, EventArgs e)
        {
            
            if (WizardWindow.m_currentPdfPage > 1)
            {
                WizardWindow.m_currentPdfPage -= 1;
                this.loadPage(WizardWindow.m_currentPdfPage);
                this.loadPage(WizardWindow.m_currentPdfPage);

            }
            else
            {
                MessageBox.Show("There is no Previous page in the current document");
              
            }



        }

        private void nextPageBtn_Click(object sender, EventArgs e)
        {

            
            if (WizardWindow.m_currentPdfPage < this.mPdfDoc.PageCount)
            {
                WizardWindow.m_currentPdfPage += 1;
                this.loadPage(WizardWindow.m_currentPdfPage);
                this.loadPage(WizardWindow.m_currentPdfPage);


            }
            else
            {
                MessageBox.Show("There is no next page in the current document");
               
            }

        }

        private void loadPage(int pageNum)
        {
            
            this.mPdfDoc.CurrentPage = pageNum;
           
            this.mPdfDoc.RenderPage(this.showPdfPicBox.Handle);

          
           Bitmap _backbuffer = new Bitmap(this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);

            using (Graphics g = Graphics.FromImage(_backbuffer))
            {
                this.mPdfDoc.DrawPageHDC(g.GetHdc());
                g.ReleaseHdc();
            }

            this.mPdfDoc.CurrentX = 0;
            this.mPdfDoc.CurrentY = 0;
            this.mPdfDoc.ClientBounds = new Rectangle(0, 0, this.mPdfDoc.PageWidth, this.mPdfDoc.PageHeight);
            this.mPdfDoc.FitToWidth(this.showPdfPicBox.Handle);
           
            this.showPdfPicBox.PictBox.Image = _backbuffer;
        
        
        }

        private void exitBtn_Click(object sender, EventArgs e)
        {


            this.Close();
        }

        private void savePdfBtn_Click(object sender, EventArgs e)
        {
            // Logic to save the pdf document in the database
            if (this.dataHeading.Text.Trim().Length > 100 || this.dataHeading.Text == String.Empty || this.mPdfDoc == null)
            {
                MessageBox.Show("Data Heading is empty or there is more the 100 character in there !! OR you have not loaded any pdf documents yet please fix it");
                return;
            
            }
            try {
               DataBaseHandler handler = new DataBaseHandler();
                // default displayoptions
               Byte option = (Byte)RotateView.Default | (Byte)FitView.FitToWidth;
                
                //
               int dataID=  handler.saveNewTxtData(this.m_path,this.dataHeading.Text, (int)DataOption.TextDataType,(Byte)option);
               handler.addDataIDOnItem(this.itemid, dataID);
               this.dHandler.updateHeading(dataID, this.dataHeading.Text);
               MessageBox.Show("Data Saved !! Window Will Close");
               this.Close();

            }
            catch(Exception ex)
            {

                MessageBox.Show("A Error has occured    " + ex.Message);
            }



        }
        
       
   
    
  }

}
